const { BASE_URL } = require('./wxRequest')
const { APPID, SECRET } = require('../utils/util')

export const wxLogin = () => {
  return new Promise((resolve, reject, complete) => {
    wx.login({
      timeout: 50 * 1000,
      success: resolve,
      fail: reject,
      complete
    })
  })
}

export const loginwithCode = (code) => {
  return new Promise((resolve, reject) => {
    let data = {
      js_code: code,
      appid: APPID,
      secret: SECRET,
      grant_type: 'authorization_code'
    }
    // wx.request({
    //   url: 'https://api.weixin.qq.com/sns/jscode2session',
    //   data,
    //   method: 'get',
    //   success: resolve,
    //   fail: reject
    // })
    
    wx.request({
      url: BASE_URL + '/user/userlogin',
      data,
      method: 'get',
      success: resolve,
      fail: reject
    })
  })
}